<?php

require_once 'modules/product/models/Product.php';
require_once 'modules/product/models/ProductCategory.php';

class product_IndexController extends Nine_Controller_Action
{
	
	public function indexAction()
	{
		$objProduct = new Models_Product();
		$objProductCategory = new Models_ProductCategory();
		
		/**
		 * Get category
		 */
		$categoryGid = $this->_getParam('cid',false);
		if (false == $categoryGid) {
			$this->_redirectToNotFoundPage();
		}
		
		$cat = $objProductCategory->getCategoryWithParent($categoryGid);
		if (null == $cat['product_category_id']) {
			$this->_redirectToNotFoundPage();
		}
		/**
         * Get number row per page
         */
		
		if ('hotel' == $cat['template']) {
			/**
			 * Template hotel, tour service
			 */
			$numRowPerPage = Nine_Registry::getConfig("hotelNumberRowPerPage");
		}
		else {
			/**
			 * Template health machine, plant, or anything
			 */
			$numRowPerPage = Nine_Registry::getConfig("helthMachineNumberRowPerPage");
			
		}
		
		$currentPage = $this->_getParam("page",1);

		/**
		 * Get all Products
		 */
		$allProducts = $objProduct->getAllEnabledProductsByCategory($categoryGid, array('sorting ASC','product_id DESC'),
                                                   $numRowPerPage,($currentPage - 1) * $numRowPerPage
                                                   );
//        echo "<pre>";print_r($allProducts);die; 
        /**
         * Modified all products
         */
                                                   
        foreach($allProducts as &$product){
        	$product['created_date'] = date(Nine_Registry::getConfig('dateFormat'),$product['created_date']);
        	if (null != $product['sale_off_start_date']) {
        		$product['sale_off_start_date'] = date(Nine_Registry::getConfig('dateFormat'),@$product['sale_off_start_date']);
        	}
        	if (null != $product['sale_off_end_date']){
        		$product['sale_off_end_date'] = date(Nine_Registry::getConfig('dateFormat'),@$product['sale_off_end_date']);
        	}

        	$product['title'] = Nine_Function::subStringAtBlank(trim(strip_tags($product['title'])),30);
        	$product['intro_text'] = Nine_Function::subStringAtBlank(trim(strip_tags($product['intro_text'])), 145);
        	$product['link'] = Nine_Route::_("product/index/detail/id/{$product['product_gid']}");
        	
        	if (null != $product['images']) {
        		$product['images'] = explode("||", $product['images']);
        		
        		if ('default' == $cat['template']) {
        			$product['main_image'] = Nine_Function::getThumbImage(@$product['images'][0], 136, 122);
        			$product['full_main_image'] = Nine_Function::getThumbImage(@$product['images'][0], 800);
        		}
        		else {
        			$product['main_image'] = Nine_Function::getThumbImage(@$product['images'][0], 218, 136);
        			$product['full_main_image'] = Nine_Function::getThumbImage(@$product['images'][0], 800);
        		}
        	}
        	else {
        		if ('default' == $cat['template']) {
        			$product['main_image'] = Nine_Function::getThumbImage("media/userfiles/images/noimage.jpg",136,122);
        		}
        		else {
        			$product['main_image'] = Nine_Function::getThumbImage("media/userfiles/images/noimage.jpg",218,136);
        		}
        		
        	}
        	$product['price'] = $objProduct->makeUpPrice($product['price']);
        }
        unset($product);
        
        /**
         * Chunk all products
         */
        if ( 9 == $numRowPerPage) {
        	$allProducts = array_chunk($allProducts, 3);
        }
        else if (8 == $numRowPerPage) {
			$allProducts = Nine_Function::arrayChunkFixed($allProducts, 2);
        }
        
        /**
         * Count all products
         */
        $count = count($objProduct->getAllEnabledProductsByCategory($categoryGid));
        
        
        $templatePath  = Nine_Registry::getModuleName() . '/views/templates/' . ((@$cat['template'])?$cat['template']:'default');
		$templatePath .= '/index.' . Nine_Constant::VIEW_SUFFIX;
		
        /**
         * Assign to rendered view
         */
        $this->view->headTitle($cat['name']);
        $this->setPagination($numRowPerPage, $currentPage, $count);
       	$this->view->cat = $cat;
        $this->view->allProducts = $allProducts;
        
        $rootCatParent = $objProductCategory->getRootParent($categoryGid);
        $this->view->menuId = "product_category_{$rootCatParent['product_category_gid']}";
		/**
		 * Render this template
		 */
		$this->view->html = $this->view->render( $templatePath );
	}
	
	
	public function detailAction() {
		
		$objProduct = new Models_Product();
		$objProductCategory = new Models_ProductCategory();
		
		$gid = $this->_getParam('id',false);
		if (false == $gid) {
			$this->_redirectToNotFoundPage();
		}
		
		$product = $objProduct->getProductById( $gid );
		if (null == $product) {
			$this->_redirectToNotFoundPage();
		}
		/**
		 * Modified product
		 */
		$product['created_date'] = date(Nine_Registry::getConfig('dateFormat'),$product['created_date']);
        if (null != $product['sale_off_start_date']) {
        	$product['sale_off_start_date'] = date(Nine_Registry::getConfig('dateFormat'),@$product['sale_off_start_date']);
        }
        if (null != $product['sale_off_end_date']){
        	$product['sale_off_end_date'] = date(Nine_Registry::getConfig('dateFormat'),@$product['sale_off_end_date']);
        }
        
        $cat = @reset($objProductCategory->getByColumns(array('product_category_gid=?'=>$product['product_category_gid']))->toArray());
		$product['category_name'] = $cat['name'];
		if ( null != @$product['images']) {
			$product['images'] = explode("||", $product['images']);
			$product['main_image'] = Nine_Function::getThumbImage(@$product['images'][0], 218);
			$product['full_main_image'] = Nine_Function::getThumbImage(@$product['images'][0], 800);

			/**
			 * Get more thumbnail images
			 */
			foreach ($product['images'] as &$image) {
			    $image = array(
			    	'thumbnail' => Nine_Function::getThumbImage($image, 102, 80),
			    	'full_image' => Nine_Function::getThumbImage($image, 800)
			    );
			}
			unset($image);

		}
        else {
        	$product['main_image'] = Nine_Function::getThumbImage("media/userfiles/images/noimage.jpg",218);
        }
		
        if (null != @$product['price']) {
        	$product['price'] = $objProduct->makeUpPrice($product['price']);
        }
        
        
		$templatePath  = Nine_Registry::getModuleName() . '/views/templates/' . ((@$cat['template'])?$cat['template']:'default');
		$templatePath .= '/detail.' . Nine_Constant::VIEW_SUFFIX;
		
        /**
         * Assign to rendered view
         */
        $this->view->headTitle($product['title']);
        $this->view->product = $product;
        
        $rootCatParent = $objProductCategory->getRootParent($product['product_category_gid']);
        
        $this->view->menuId = "product_category_{$rootCatParent['product_category_gid']}";

        /**
		 * Render this template
		 */
		$this->view->html = $this->view->render( $templatePath );
	}
	
	
	private function _redirectToNotFoundPage()
	{
	    $this->_redirect("");
	}
	
}